/**
 * Copyright 2011 FeedDreamwork SIG
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package org.feeddreamwork.processors;

import java.io.*;
import java.util.*;

import org.feeddreamwork.*;
import org.feeddreamwork.feed.*;
import org.feeddreamwork.fetcher.*;
import org.w3c.dom.*;

public class MergeEcnuFeedProcessor implements FeedProcessor {
	private String url;

	public MergeEcnuFeedProcessor(String url) {
		Utils.throwIfNullOrEmpty(url);
		this.url = url;
	}

	@Override
	public void process(FeedContext context) throws FeedProcessException {
		Utils.throwIfNull(context);
		Utils.throwIfNull(context.getFeed());

		try {
			HttpFetcher fetcher = new HttpFetcher(url);
			Feed result = ecnuParse(fetcher.getContent(), url);
			for (Entry entry : result.getEntries())
				context.getFeed().getEntries().add(entry.clone());
		} catch (IOException e) {
			throw new FeedProcessException();
		}
	}

	private Feed ecnuParse(String content, String url)
			throws FeedProcessException {
		Feed feed = new Feed("ECNU", url);
		try {
			String sign = "<td><hr color='#6699CC'></td>";
			content = content.substring(content.indexOf(sign) + sign.length());
			content = content.substring(content.indexOf(sign) + sign.length());
			content = content.substring(content.indexOf("</tr>") + 5);
			content = content.substring(0,
					content.indexOf("<td height=\"20\">&nbsp;</td>"));
			content = content.substring(0, content.lastIndexOf("<tr>"));
			content = "<tr>" + content;
			content = content.replaceAll("<br>", "");
			content = content.replaceAll("<td><a href", "<![CDATA[<a href");
			content = content.replaceAll("</td>", "]]>");

			Document doc = XMLUtils.parseDocument(content);
			Node article = doc.getFirstChild();
			NodeList articleList = article.getChildNodes();
			for (int i = 0; i < articleList.getLength(); i++)
				if (articleList.item(i).getNodeName().equals("tr")) {
					String toProcess = articleList.item(i).getTextContent()
							.trim();
					Date date = DateUtils.parseOnlyDate(toProcess.substring(
							toProcess.indexOf("</a>(") + 5).substring(0, 9));
					String link = null;
					if (toProcess.contains("a href=#"))
						link = "http://www.ecnu.edu.cn/new_gonggao/"
								+ toProcess
										.substring(toProcess
												.indexOf("window.open('") + 13,
												toProcess.indexOf("','','"));
					else
						link = toProcess.substring(
								toProcess.indexOf("<a href=") + 8,
								toProcess.indexOf(" target="));
					String title = toProcess.substring(
							toProcess.indexOf(">") + 1,
							toProcess.indexOf("</a>"));
					feed.getEntries().add(
							new Entry(title, link, "", "ECNU", date));
				}
			return feed;
		} catch (Exception e) {
			throw new FeedProcessException();
		}
	}
}
